package com.net355.eblog.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateToolkit {
	private static final String[] zodiacArr = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊" };
	private static final String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };
	private static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };
	static {
		Locale.setDefault(Locale.CHINA);
	}

	public static String getHello() {
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		if (hour < 5) {
			return "凌晨好";
		} else if (hour < 11) {
			return "上午好";
		} else if (hour < 13) {
			return "中午好";
		} else if (hour < 19) {
			return "下午好";
		} else {
			return "晚上好";
		}
	}

	public static Date getNearestTime(Date startDate, String[] days, String[] hms) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hms[0]));
		c.set(Calendar.MINUTE, Integer.parseInt(hms[1]));
		c.set(Calendar.SECOND, Integer.parseInt(hms[2]));
		for (int i = 0; i < days.length; i++) {
			c.set(Calendar.DAY_OF_WEEK, Integer.parseInt(days[i]));
			if (c.getTime().after(startDate)) {
				return c.getTime();
			}
		}
		c.set(Calendar.DAY_OF_WEEK, Integer.parseInt(days[0]));
		return DateToolkit.getDateByDay(c.getTime(), 7);
	}

	/**
	 * 获取当前时间的format格式
	 * 
	 * @param Format
	 * @return
	 */
	public static String formatDate(String Format) {
		Date d = Calendar.getInstance().getTime();
		try {
			SimpleDateFormat f = new SimpleDateFormat(Format);
			return f.format(d);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 获取指定日期的Format格式
	 * 
	 * @param date
	 * @param Format
	 * @return
	 */
	public static String formatDate(Date date, String Format) {
		try {
			if (date == null) {
				return "";
			}
			SimpleDateFormat f = new SimpleDateFormat(Format);
			return f.format(date);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 生成时间
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @return
	 */
	public static Date strToDate(String Formate, String date) {
		try {
			SimpleDateFormat f = new SimpleDateFormat(Formate);
			return f.parse(date);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 生成时间
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @return
	 */
	public static Date strToDate(int year, int month, int date) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DATE, date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	/**
	 * 生成时间
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Date strToDate(int year, int month, int date, int hour, int minute, int second) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DATE, date);
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, minute);
		c.set(Calendar.SECOND, second);
		return c.getTime();
	}

	/**
	 * 获取指定日期的月份天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonthNum(Date date) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date);
		c1.set(Calendar.DATE, 1);
		c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH) + 1, 1);
		return c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取当前时间的星期值
	 * 
	 * @return
	 */
	public static String getWeek() {
		String weekinfo = "";
		int week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		switch (week) {
		case 1:
			weekinfo = "星期日";
			break;
		case 2:
			weekinfo = "星期一";
			break;
		case 3:
			weekinfo = "星期二";
			break;
		case 4:
			weekinfo = "星期三";
			break;
		case 5:
			weekinfo = "星期四";
			break;
		case 6:
			weekinfo = "星期五";
			break;
		case 7:
			weekinfo = "星期六";
			break;
		default:
			weekinfo = "";
		}
		return weekinfo;
	}

	/**
	 * 获取当天的开始时间
	 * 
	 * @return
	 */
	public static Date getCurrentDateStart() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取当天时间的结束时间
	 * 
	 * @return
	 */
	public static Date getCurrentDateEnd() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 获取当天时间的下一个月的起始时间的第5s
	 * 
	 * @return
	 */
	public static Date getNextMonthDateStart() {
		Calendar _calendar = Calendar.getInstance();
		_calendar.set(Calendar.MONTH, _calendar.get(Calendar.MONTH) + 1);
		_calendar.set(Calendar.DATE, 1);
		_calendar.set(Calendar.HOUR_OF_DAY, 0);
		_calendar.set(Calendar.MINUTE, 0);
		_calendar.set(Calendar.SECOND, 5);
		return _calendar.getTime();
	}

	/**
	 * 获取当天时间的下一年的起始时间的第10s
	 * 
	 * @return
	 */
	public static Date getNextYearDateStart() {
		Calendar _calendar = Calendar.getInstance();
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR) + 1);
		_calendar.set(Calendar.MONTH, 0);
		_calendar.set(Calendar.DATE, 1);
		_calendar.set(Calendar.HOUR_OF_DAY, 0);
		_calendar.set(Calendar.MINUTE, 0);
		_calendar.set(Calendar.SECOND, 10);
		return _calendar.getTime();
	}

	/**
	 * 获取指定时间当天的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateStart(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, _calendar.get(Calendar.MONTH));
		_calendar.set(Calendar.DATE, _calendar.get(Calendar.DATE));
		_calendar.set(Calendar.HOUR_OF_DAY, 0);
		_calendar.set(Calendar.MINUTE, 0);
		_calendar.set(Calendar.SECOND, 0);
		return _calendar.getTime();
	}

	/**
	 * 获取指定时间当天的结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateEnd(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, _calendar.get(Calendar.MONTH));
		_calendar.set(Calendar.DATE, _calendar.get(Calendar.DATE));
		_calendar.set(Calendar.HOUR_OF_DAY, 23);
		_calendar.set(Calendar.MINUTE, 59);
		_calendar.set(Calendar.SECOND, 59);
		return _calendar.getTime();
	}

	/**
	 * 获取指定时间当月的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMonthStart(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, _calendar.get(Calendar.MONTH));
		_calendar.set(Calendar.DATE, 1);
		_calendar.set(Calendar.HOUR_OF_DAY, 0);
		_calendar.set(Calendar.MINUTE, 0);
		_calendar.set(Calendar.SECOND, 0);
		return _calendar.getTime();
	}

	/**
	 * 获取指定时间当月的结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMonthEnd(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, _calendar.get(Calendar.MONTH) + 1);
		_calendar.set(Calendar.DATE, 0);
		_calendar.set(Calendar.HOUR_OF_DAY, 23);
		_calendar.set(Calendar.MINUTE, 59);
		_calendar.set(Calendar.SECOND, 59);
		return _calendar.getTime();
	}

	/**
	 * 获取指定时间当年的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearStart(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, 0);
		_calendar.set(Calendar.DATE, 1);
		_calendar.set(Calendar.HOUR_OF_DAY, 0);
		_calendar.set(Calendar.MINUTE, 0);
		_calendar.set(Calendar.SECOND, 0);
		return _calendar.getTime();
	}
	
	/**
	 * 获取指定时间当年的6月份结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearHalfEnd(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, 6);
		_calendar.set(Calendar.DATE, 0);
		_calendar.set(Calendar.HOUR_OF_DAY, 23);
		_calendar.set(Calendar.MINUTE, 59);
		_calendar.set(Calendar.SECOND, 59);
		return _calendar.getTime();
	}
	
	/**
	 * 获取指定时间当年的7月份开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearHalfStart(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, 6);
		_calendar.set(Calendar.DATE, 1);
		_calendar.set(Calendar.HOUR_OF_DAY, 0);
		_calendar.set(Calendar.MINUTE, 0);
		_calendar.set(Calendar.SECOND, 0);
		return _calendar.getTime();
	}

	/**
	 * 获取指定时间当年的结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearEnd(Date date) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		_calendar.set(Calendar.MONTH, 12);
		_calendar.set(Calendar.DATE, 0);
		_calendar.set(Calendar.HOUR_OF_DAY, 23);
		_calendar.set(Calendar.MINUTE, 59);
		_calendar.set(Calendar.SECOND, 59);
		return _calendar.getTime();
	}

	/**
	 * 获取当前时间offset秒后的时间
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date getDateAfterSec(Date date, int offset) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.add(Calendar.SECOND, offset);
		return _calendar.getTime();
	}

	/**
	 * 获取几天后的时间
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date getDateByDay(Date date, int offset) {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(date);
		_calendar.add(Calendar.DATE, offset);
		return _calendar.getTime();
	}

	/**
	 * 比较两个日期相差的天时分
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static String getStringByDuration(long duration) {
		if (duration < 60000) {
			return "<1分钟";
		}
		String result = "";
		long day = duration / (3600 * 24 * 1000);
		duration = duration - day * 3600 * 24 * 1000;
		result += day == 0 ? "" : day + "天";
		long hour = duration / (3600 * 1000);
		duration = duration - hour * 3600 * 1000;
		result += (hour == 0 && day == 0) ? "" : hour + "小时";
		long min = duration / 60000;
		result += min + "分钟";
		return result;
	}

	/**
	 * 计算两个日期long的差值
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static long getTwoDateIntervalLong(Date start, Date end) {
		return end.getTime() - start.getTime();
	}

	/**
	 * 根据日期获取生肖
	 * 
	 * @return
	 */
	public static String date2Zodica(Calendar time) {
		return zodiacArr[time.get(Calendar.YEAR) % 12];
	}

	/**
	 * 根据日期获取星座
	 * 
	 * @param time
	 * @return
	 */
	public static String date2Constellation(Calendar time) {
		int month = time.get(Calendar.MONTH);
		int day = time.get(Calendar.DAY_OF_MONTH);
		if (day < constellationEdgeDay[month]) {
			month = month - 1;
		}
		if (month >= 0) {
			return constellationArr[month];
		}
		// default to return 魔羯
		return constellationArr[11];
	}
	
	
}
